package com.pay.listener;

import com.alipay.api.response.AlipayTradeRefundResponse;
import com.pay.service.AlipayService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author lins
 * @date 2021/10/21 14:50
 */
@Slf4j
@Service
@RabbitListener(queues = "rebund")
public class RebundServiceListener {
    @Autowired
    AlipayService alipayService;
    @RabbitHandler
    public void rebund(Integer examineId, Channel channel, Message message) throws IOException {
        try {
            System.out.println("收到审核未通过的id>>>>>>"+examineId+">>>>>>>准备退款");
            AlipayTradeRefundResponse rebund = alipayService.rebund(examineId);
            if (rebund.isSuccess()){
                log.info("订单>>>>>>"+rebund.getOutTradeNo()+">>>>>>退款成功");
            }else {
                log.error("订单>>>>>>"+rebund.getOutTradeNo()+">>>>>>退款失败");
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
